package com.shixu.framework.plugins.weixin.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.shixu.framework.plugins.weixin.bean.Articles;
import com.shixu.framework.plugins.weixin.bean.OutMessage;


/**
 * 被动回复用户消息
 * @author luy
 *
 */
public class PassiveNewsService {
	
	/** 回复文本消息 */
	public static final String NEWSTYPE_TEXT = "text";
	/** 回复图文消息 */
	public static final String NEWSTYPE_NEWS = "news";
	
	
	/**
	 * 编译文本信息
	 * @param toUser 接收方帐号（收到的OpenID）
	 * @param fromUser 开发者微信号
	 * @param content 回复的消息内容（换行：在content中能够换行，微信客户端就支持换行显示）
	 * @return
	 */
	public String getBackXMLTypeText(OutMessage message,String content) {
		
		/*<xml>
		<ToUserName><![CDATA[toUser]]></ToUserName>
		<FromUserName><![CDATA[fromUser]]></FromUserName>
		<CreateTime>12345678</CreateTime>
		<MsgType><![CDATA[text]]></MsgType>
		<Content><![CDATA[你好]]></Content>
		</xml>*/

		Document document = DocumentHelper.createDocument();
		Element root = document.addElement("xml");
		
		root.addElement("ToUserName").addText("<![CDATA["+message.getToUserName()+"]]>");
		root.addElement("FromUserName").addText("<![CDATA["+message.getFromUserName()+"]]>");
		root.addElement("CreateTime").addText(message.getCreateTime().toString());
		root.addElement("MsgType").addText("<![CDATA["+NEWSTYPE_TEXT+"]]>");
		root.addElement("Content").addText("<![CDATA["+content+"]]>");
		
		return root.asXML();
	}  
	
	/**
	 * 编译文本信息
	 * @param toUser 接收方帐号（收到的OpenID）
	 * @param fromUser 开发者微信号
	 * @param content 回复的消息内容（换行：在content中能够换行，微信客户端就支持换行显示）
	 * @return
	 */
	public String getBackXMLTypeNews(OutMessage message,List<Articles> articles) {
		
		/*
		<xml>
			<ToUserName><![CDATA[toUser]]></ToUserName>
			<FromUserName><![CDATA[fromUser]]></FromUserName>
			<CreateTime>12345678</CreateTime>
			<MsgType><![CDATA[news]]></MsgType>
			<ArticleCount>2</ArticleCount>
			<Articles>
				<item>
					<Title><![CDATA[title1]]></Title> 
					<Description><![CDATA[description1]]></Description>
					<PicUrl><![CDATA[picurl]]></PicUrl>
					<Url><![CDATA[url]]></Url>
				</item>
				<item>
					<Title><![CDATA[title]]></Title>
					<Description><![CDATA[description]]></Description>
					<PicUrl><![CDATA[picurl]]></PicUrl>
					<Url><![CDATA[url]]></Url>
				</item>
			</Articles>
		</xml> */

		//创建一个文档对象
		Document document = DocumentHelper.createDocument();
		//创建一个根节点
		Element root = document.addElement("xml");
		
		root.addElement("ToUserName").addText("<![CDATA["+message.getToUserName()+"]]>");
		root.addElement("FromUserName").addText("<![CDATA["+message.getFromUserName()+"]]>");
		root.addElement("CreateTime").addText(message.getCreateTime().toString());
		root.addElement("MsgType").addText("<![CDATA["+NEWSTYPE_NEWS+"]]>");
		root.addElement("ArticleCount").addText(articles.size()+"");
		
		//创建一个子节点
		Element articlesElement = root.addElement("Articles");
		
		for (Articles item : articles) {
			Element itemElement = articlesElement.addElement("item");
			itemElement.addElement("Title").addText("<![CDATA["+item.getTitle()+"]]>");
			itemElement.addElement("Description").addText("<![CDATA["+item.getDescription()+"]]>");
			itemElement.addElement("PicUrl").addText("<![CDATA["+item.getPicUrl()+"]]>");
			itemElement.addElement("Url").addText("<![CDATA["+item.getUrl()+"]]>");
		}
		
		return root.asXML();
	}  

	
	  

	/**
	 * 测试
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		//创建OutMessage
		OutMessage message = new OutMessage();
		message.setToUserName("toUserName");
		message.setFromUserName("fromUserName");
		message.setCreateTime(new Date().getTime()/100000);
		
		//创建Articles
		Articles articles1 = new Articles();
		articles1.setTitle("测试1");
		articles1.setDescription("描述1");
		articles1.setPicUrl("http://image.baidu.com/i?ct=503316480&z=0&tn=baiduimagedetail&ipn=d&word=%E5%8D%A1%E9%80%9A&step_word=&pn=23&spn=0&di=98015698550&pi=&rn=1&is=&istype=2&ie=utf-8&oe=utf-8&in=24901&cl=2&lm=-1&st=-1&cs=1199518288%2C2898646180&os=691834627%2C255550650&adpicid=0&ln=1000&fr=&fmq=1425353405299_R&ic=0&s=&se=1&sme=0&tab=&width=&height=&face=0&ist=&jit=&cg=&objurl=http%3A%2F%2Fpic23.nipic.com%2F20120831%2F10705080_084938267132_2.jpg&fromurl=ippr_z2C%24qAzdH3FAzdH3Fooo_z%26e3Bgtrtv_z%26e3Bv54AzdH3Ffi5oAzdH3FdAzdH3Fm8AzdH3Fm000nnmh1waaamu8_z%26e3Bip4s");
		articles1.setUrl("www.baidu.com");
		
		Articles articles2 = new Articles();
		articles2.setTitle("测试2");
		articles2.setDescription("描述2");
		articles2.setPicUrl("http://image.baidu.com/i?ct=503316480&z=0&tn=baiduimagedetail&ipn=d&word=%E5%8D%A1%E9%80%9A&step_word=&pn=41&spn=0&di=112985480300&pi=&rn=1&is=&istype=2&ie=utf-8&oe=utf-8&in=18703&cl=2&lm=-1&st=-1&cs=235752461%2C436342863&os=556404912%2C528690071&adpicid=0&ln=1000&fr=&fmq=1425353405299_R&ic=0&s=&se=1&sme=0&tab=&width=&height=&face=0&ist=&jit=&cg=&objurl=http%3A%2F%2Fbizhi.zhuoku.com%2F2009%2F09%2F29%2Fkatong%2F1680x1050.jpg&fromurl=ippr_z2C%24qAzdH3FAzdH3Fooo_z%26e3Bzi75h7_z%26e3Bv54AzdH3Fzi754twgktzitAzdH3F3tg2x7wg-3tg2x7wgytp7AzdH3Fdaalaldl8cnccn%25db0%25dl_z%26e3Bip4");
		articles2.setUrl("www.baidu.com");
		
		List<Articles> articles = new ArrayList<Articles>();
		articles.add(articles1);
		articles.add(articles2);
		
		//实例化操作类
		PassiveNewsService news = new PassiveNewsService();
		
		//定义返回值
		String result = null;
		
		/* 测试编译文本信息 */
		result = news.getBackXMLTypeText(message,"neirong");
	    System.out.println(result); 
	    
	    /* 测试编译文本信息 */
		result = news.getBackXMLTypeNews(message, articles);
	    System.out.println(result);
	  
	}  

}
